// 这里是有关用户的中间件
// 导入正则
let { testUserName, testPassWord, testNickName } = require('../gather/regs')
let { parseToken } = require('../gather/token')
//这里是对注册参数的验证
exports.testRegister = (req, res, next) => {
    let { username, password, rpwd } = req.body
    if (!username || !password || !rpwd) return next(4)
    if (password !== rpwd) return res.send({ code: 0, message: '两次密码输入不一致~~~' })
    // 然后需要制定正则
    if (!testUserName(username) || !testPassWord(password)) return next(5)
    next()
}
// 这里是登录的参数的验证
exports.testLogin = (req, res, next) => {
    let { username, password } = req.body
    if (!username || !password) return next(4)
    // 然后需要制定正则
    if (!testUserName(username) || !testPassWord(password)) return next(5)
    next()
}
// 验证用户id的中间件
exports.testId = (req, res, next) => {
    let UserId = req.body
    if (UserId.length == 0) return res.send({ code: 0, message: '你传递的id有错误' })
    next()
}
// 验证修改用户信息的中间件
exports.testinfo = (req, res, next) => {
    let { id } = req.body
    if (id.length == 0) return res.send({ code: 0, message: '你传递的id有错误' })
    next()
}
// 验证token 
exports.testToken = async (req, res, next) => {
    let { authorization } = req.headers
    // 判断是否传递token
    if (!authorization) return next(3)
    // 这里进行解密
    let reslut = await parseToken(authorization)
    console.log(reslut)
    if (reslut.code !== 1) return res.send({ code: 0, message: 'token失效了,请重新登录' })
    next()
}
exports.testname = (req, res, next) => {
    console.log(res);

    next()
}